<?php

class PrintCalendario {
	private $day_names = array( "Lu", "Ma", "Mi", "Ju", "Vi", "Sá", "Do");
 	private $day_fnames = array( "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sábado", "Domingo");
	private $month_names = array( "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");

	function generate_calendar($year, $month, $days = array(), $first_day = 1) {
		$first_of_month = gmmktime( 0, 0, 0, $month, 1, $year);

// 		list($month, $year, $weekday) = explode(',',gmstrftime('%m,%Y,%w',$first_of_month));
		list($weekday) = gmstrftime('%w',$first_of_month);
// 		$month_name = $this->month_names[ intval($month) - 1];

		$weekday = ($weekday + 7 - $first_day) % 7; 				// Ajustar el desplazamiento del primer día
// 		$title   = htmlentities( ucfirst($month_name)).'&nbsp;'.$year; 		// Título

		// previous and next links, if applicable
		@list($p, $pid) = each($pn); 
		@list($n, $nid) = each($pn); 

		$calendar = '<table year="'.$year.'" month="'.$month.'" class="mini_calendar"><tr>';

		
		foreach($this->day_names as $d)
			$calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($d).'</th>';
		$calendar .= '</tr><tr class="tr_num">';

		if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'; 	// Dias iniciales vacíos.

		for( $day = 1, $days_in_month = gmdate('t',$first_of_month); $day <= $days_in_month; $day++, $weekday++){
			if($weekday == 7){
				$weekday   = 0; 			// Comenzar una nueva semana.
				$calendar .= '</tr><tr class="tr_num">';
			}
			if( isset( $days[ $day]) and is_array( $days[ $day])){
				@list($link, $classes, $content) = $days[$day];
				if(is_null($content))  $content  = $day;
				$calendar .= "<td day=\"$day\" ".($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
					($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
			}else 
				$calendar .= "<td day=\"$day\" >$day</td>";
		}

		if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>';  	// Llenar dias vacios.

		return $calendar."</tr>\n</table>\n";
	}

	function generate_tittle( $year, $month, $day = 0) {
		$month_name = $this->month_names[ intval($month) - 1];
		$title = ($day ? $day.'&nbsp;' : '').htmlentities( ucfirst($month_name)).'&nbsp;'.$year; 		// Título
		return $title;
	}

	function generate_week_by_day( $year, $month, $day, $days = array()) {
		$gmmk_day = gmmktime( 0, 0, 0, $month, $day, $year);

		list($month, $year, $weekday) = explode(',',gmstrftime('%m,%Y,%w',$gmmk_day));
		$month_name = $this->month_names[ intval($month) - 1];

		// $weekday
		// 1 -> 0 = Lunes
		// 2 -> 1 = Martes
		// 3 -> 2 = Miercoles
		// 4 -> 3 = Jueves
		// 5 -> 4 = Viernes
		// 6 -> 5 = Sábado
		// 0 -> 6 = Domingo
		$weekday = ($weekday + 6) % 7;
		$title   = htmlentities( ucfirst( substr($month_name, 0, 3))).'&nbsp;'.$year; 		// Título

		// previous and next links, if applicable
		@list($p, $pl) = each($pn); 
		@list($n, $nl) = each($pn); 

		$week = '<table class="week_by_day"><tr>';

		$pdia = $day - $weekday;
		$udia = $day - $weekday + 6;
		
		$i = $pdia;
		$week .= '<td class="td_time"></td>';
		foreach($this->day_fnames as $d) {
			$week.= '<th abbr="'.htmlentities($d).'">'.htmlentities($d).'<br />'.($i < 10 ? '0'.$i: $i).' '.$title.'</th>';
			$i++;
		}
		$week .= '</tr><tr>';
		$week .= '<td class="td_time"></td>';
		for( $i = $pdia; $i <= $udia; $i++)
			$week .= '<td class="td_day"></td>';
		return $week.'</tr></table>';
	}

	function generate_week_by_time( $year, $month, $day, $ls_event) {
		if( $ls_event == NULL) $ls_event = array();
		
		date_default_timezone_set('America/Santiago');

		$hoy = date('Y-m-d H:i:s', time());
		$th = strtotime( $hoy);

		$gmmk_day = gmmktime( 0, 0, 0, $month, $day, $year);

		list($month, $year, $weekday) = explode(',',gmstrftime('%m,%Y,%w',$gmmk_day));
		$month_name = $this->month_names[ intval($month) - 1];

		$first_of_month = gmmktime( 0, 0, 0, $month, 1, $year);
		$days_in_month = gmdate('t',$first_of_month);

		// $weekday
		// 1 -> 0 = Lunes
		// 2 -> 1 = Martes
		// 3 -> 2 = Miercoles
		// 4 -> 3 = Jueves
		// 5 -> 4 = Viernes
		// 6 -> 5 = Sábado
		// 0 -> 6 = Domingo
		$weekday = ($weekday + 6) % 7;

		// previous and next links, if applicable
		@list($p, $pl) = each($pn); 
		@list($n, $nl) = each($pn); 

		$pdia = $day - $weekday;
		$udia = $day - $weekday + 6;

		$week = '<table class="week_by_time"><tr>';
		
		for( $i = 0; $i < 24; $i++) {
			$h = ($i < 10) ? "0".$i : $i;
			
			$week .= '<td class="td_time"><div class="div_htime">'.$h.'<span class="sp_time">00</span></div></td>';
			for( $j = $pdia; $j <= $udia; $j++) {
				$week .= '<td hour="'.$h.':00" day="'.$j.'" month="'.$month.'" year="'.$year.'"class="td_mod1">';

				$datetime1 = $year."-".$month."-".$j." ".$h.":00:00";

				$t1 = strtotime( $datetime1);
				$t2 = $t1 + 900;

				$week .= ($t1 <= $th && $th < $t2) ? sprintf('<div class="div_line" style="top:%spx;"></div>', ($th - $t1)/105 -1) : '';
				foreach( $ls_event as $e) {
					$te = strtotime( $e['Even_fini']);

					if( $te >= $t1 && $te < $t2) {
						$tdiff = (strtotime( $e['Even_ffin']) - $te) / 106;
						$week .= sprintf( "<div class=\"div_tev\" style=\"height:%spx; \" >%s</div>", $tdiff, $e['Even_titulo'] );
					}
				}

				$week .= '</td>';
			}
			$week .= ($i < 10) ? '</tr><tr>' : '</tr><tr hour="'.$i.':15">';
		
			$week .= '<td class="td_time"></td>';
			for( $j = $pdia; $j <= $udia; $j++) {
				$week .= '<td hour="'.$h.':15" day="'.$j.'" month="'.$month.'" year="'.$year.'"class="td_mod2">';

				$datetime1 = $year."-".$month."-".$j." ".$h.":15:00";

				$t1 = strtotime( $datetime1);
				$t2 = $t1 + 900;

				$week .= ($t1 <= $th && $th < $t2) ? sprintf('<div class="div_line" style="top:%spx;"></div>', ($th - $t1)/105 -1) : '';
				foreach( $ls_event as $e) {
					$te = strtotime( $e['Even_fini']);

					if( $te >= $t1 && $te < $t2) {
						$tdiff = (strtotime( $e['Even_ffin']) - $te) / 106;
						$week .= sprintf( "<div class=\"div_tev\" style=\"height:%spx\" >%s</div>", $tdiff, $e['Even_titulo'] );
					}
				}
				$week .= '</td>';

			}

			$week .= '</tr><tr>';

			$week .= '<td class="td_time"></td>';
			for( $j = $pdia; $j <= $udia; $j++) {
				$week .= '<td hour="'.$h.':30" day="'.$j.'" month="'.$month.'" year="'.$year.'"class="td_mod3">';

				$datetime1 = $year."-".$month."-".$j." ".$h.":30:00";

				$t1 = strtotime( $datetime1);
				$t2 = $t1 + 900;
				
				$week .= ($t1 <= $th && $th < $t2) ? sprintf('<div class="div_line" style="top:%spx;"></div>', ($th - $t1)/105 -1) : '';
				foreach( $ls_event as $e) {
					$te = strtotime( $e['Even_fini']);

					if( $te >= $t1 && $te < $t2) {
						$tdiff = (strtotime( $e['Even_ffin']) - $te) / 106;
						$week .= sprintf( "<div class=\"div_tev\" style=\"height:%spx\" >%s</div>", $tdiff, $e['Even_titulo'] );
					}
				}
				$week .= '</td>';
			}
			$week .= '</tr><tr>';

			$week .= '<td class="td_time"></td>';
			for( $j = $pdia; $j <= $udia; $j++) {
				$week .= '<td hour="'.$h.':45" day="'.$j.'" month="'.$month.'" year="'.$year.'"class="td_mod4">';

				$datetime1 = $year."-".$month."-".$j." ".$h.":45:00";

				$t1 = strtotime( $datetime1);
				$t2 = $t1 + 900;
				
				$week .= ($t1 <= $th && $th < $t2) ? sprintf('<div class="div_line" style="top:%spx;"></div>', ($th - $t1)/105 -1) : '';
				foreach( $ls_event as $e) {
					$te = strtotime( $e['Even_fini']);

					if( $te >= $t1 && $te < $t2) {
						$tdiff = (strtotime( $e['Even_ffin']) - $te) / 106;
						$week .= sprintf( "<div class=\"div_tev\" style=\"height:%spx\" >%s</div>", $tdiff, $e['Even_titulo'] );
					}
				}
				$week .= '</td>';
			}
			$week .= ($i < 23) ? '</tr><tr>': '';
		}
		return $week.'</tr></table>';
	}

	function get_month_names() {
		return json_encode( $this->month_names);
	}

}

/*
Highlight the current day

A few people have asked if it is possible to do this. The code is built to be extensible so you can do whatever you want with it.

July 2012
Sun	Mon	Tue	Wed	Thu	Fri	Sat
1	2	3	4	5	6	7
8	9	10	11	12	13	14
15	16	17	18	19	20	21
22	23	24	25	26	27	28
29	30	31	 
Code

$time = time(); 
$today = date('j',$time); 
$days = array($today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger; text-decoration: blink;">'.$today.'</span>')); 
echo generate_calendar(date('Y', $time), date('n', $time), $days); 

---------

Link or provide content for certain days

January 2004
Sun	Mon	Tue	Wed	Thu	Fri	Sat
 	1	2	3
4	5	6	7	8	9	10
11	12	13	14	15	16	17
18	19	20	21	22	23	24
25	twenty-six	27	28	29	30	31
Code
    $days = array( 
        2=>array('/weblog/archive/2004/Jan/02','linked-day'), 
        3=>array('/weblog/archive/2004/Jan/03','linked-day'), 
        8=>array('/weblog/archive/2004/Jan/08','linked-day'), 
        22=>array('/weblog/archive/2004/Jan/22','linked-day'), 
        26=>array(NULL,'linked-day textual','twenty-six'), 
    ); 
    echo generate_calendar(2004, 1, $days, 3, '/weblog/archive/2004/Jan')

----------

With previous and next links (new in 2.3)
    $pn = array('&laquo;'=>'/weblog/archive/2004/Jul', '&raquo;'=>'/weblog/archive/2004/Sep');
    echo generate_calendar(2004, 8, NULL, 3, NULL, 0, $pn); 
*/

?>
